$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'business/qualhonor/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '资质荣誉名称', name: 'name', index: 'name', width: 80 },
			{ label: '资质荣誉图片', name: 'imageUrl', index: 'image_url', width: 80 , formatter: function (value, options, row) {
                    return ' <img src="'+value+'" style="width:80px" />';
                }},
			{ label: '排序值', name: 'sort', index: 'sort', width: 80 }, 			
			{ label: '类型', name: 'type', index: 'type', width: 80 , formatter: function (value, options, row) {
                    return value === '0' ? "集团荣誉":"集团资质";
                }
            },
			{ label: '创建人', name: 'creator', index: 'creator', width: 80 }, 			
			{ label: '修改人', name: 'updator', index: 'updator', width: 80 },
			{ label: '是否可用，0----可用，1-----已删除', name: 'enabled', index: 'enabled', width: 80 , formatter: function (value, options, row) {
                    return value === 0 ?
                        '<span class="label label-success">可用</span>' :
                        '<span class="label label-danger">已删除</span>';
                }
            },
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 }, 			
			{ label: '修改时间', name: 'updateTime', index: 'update_time', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    $(".selectpicker").selectpicker({noneSelectedText: '--请选择--' });
    new AjaxUpload('#upload', {
        action: baseURL + 'upload/image',
        name: 'file',
        autoSubmit: true,
        responseType: "json",
        onSubmit: function (file, extension) {
            if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))) {
                alert('只支持jpg、png、gif格式的图片！');
                return false;
            }
        },
        onComplete: function (file, r) {
            // $("#imageUrlDiv").html('<img id="imageUrl" style="width:300px;margin-bottom:10px" src="http://112.1.21.66:8088/images/hrtraining_img.jpg"><i class="imageDel" onclick="deleteImage();">×</i>');
            if (r.code == 0) {
                //alert(r.url);
                $("#imageUrlDiv").html('<img id="imageUrl" style="width:300px;margin-bottom:10px" src="'+r.data+'"><i class="imageDel" onclick="deleteImage();">×</i>');
            } else {
                alert(r.msg);
            }
        }
    });
});

function deleteImage() {
    $(".imageBg").html("");
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		qualHonor: {},
		typeList:[{
		    "code":0,
            "value":"集团荣誉"
        },{
            "code":1,
            "value":"集团资质"
        }]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.qualHonor = {};
            $("#imageUrlDiv").html("");
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            // vm.getInfo(id);

            vm.getInfoTongBu(id,function () {
                if (vm.qualHonor.imageUrl != '' && vm.qualHonor.imageUrl != undefined) {
                    console.log(vm.qualHonor.type);
                    $("#imageUrlDiv").html('<img id="imageUrl" style="width:300px;margin-bottom:10px" src="'+vm.qualHonor.imageUrl+'"><i class="imageDel" onclick="deleteImage();">×</i>');
                    $('.selectpicker').selectpicker('val', vm.qualHonor.type);
                }
            })
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.qualHonor.id == null ? "business/qualhonor/save" : "business/qualhonor/update";
                vm.qualHonor.imageUrl = $('#imageUrl').attr("src");
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.qualHonor),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "business/qualhonor/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "business/qualhonor/info/"+id, function(r){
                vm.qualHonor = r.qualHonor;
            });
		},
        getInfoTongBu: function(id,callback){
            $.get(baseURL + "business/qualhonor/info/"+id, function(r){
                vm.qualHonor = r.qualHonor;
                callback();
            });
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});